package main

import "fmt"

// map初始化方式:
// 1.make+赋值
// 2.直接赋值
// 3.map 循环只能用range
// map的crud：
// map的复杂类型：
func main() {

	defineWithMakeAndAssign()
	defineWithDirectAssign()
	mapCrud()

	complexMap()
}

// defineWithMakeAndAssign
//
//	@Description: map初始化方式:make+赋值
func defineWithMakeAndAssign() {
	fmt.Println("-------------map初始化方式:make+赋值-------------")
	fmt.Println()
	var m2 map[int]int = make(map[int]int)
	m2[1] = 1
	m2[2] = 2
	m2[1] = 0
	for key, value := range m2 {
		fmt.Println("key=", key, "value=", value)
	}
	fmt.Println()
	for key := range m2 {
		ele, ok := m2[key]
		if ok {
			fmt.Println("key=", key, "value=", ele)
		}

	}
}

// defineWithDirectAssign
//
//	@Description: map初始化方式:直接赋值
func defineWithDirectAssign() {
	fmt.Println("-------------map初始化方式:直接赋值-------------")
	var m2 map[string]float64 = map[string]float64{
		"key1": 1.1,
		"key2": 2.2,
		"key3": 3.3}
	fmt.Println(m2)
	for s, f := range m2 {
		fmt.Printf("key=%s,value=%.2f \n", s, f)
	}

}

// mapCrud
//
//	@Description: map的增删改查
func mapCrud() {
	fmt.Println("-------------map的增删改查-------------")
	var m1 map[float64]string = map[float64]string{
		1.1: "tom",
		1.2: "jack",
		1.3: "rose",
	}
	fmt.Println("map定义完成=", m1)
	//增加create
	m1[1.4] = "lucas"
	m1[1.5] = "tristan"
	fmt.Println("map增加ele=", m1)
	//修改update
	m1[1.1] = "tom-changed"
	fmt.Println("map修改ele=", m1)
	//删除delete
	delete(m1, 1.4)
	fmt.Println("map删除ele=", m1)

	//查询~
	var str, ok = m1[1.1]
	if ok {
		fmt.Println("key=1.1,value=", str)
	} else {
		fmt.Println("key=1.1不存在")
	}
	var str2, ok2 = m1[1.4]
	if ok2 {
		fmt.Println("key=1.4,value=", str2)
	} else {
		fmt.Println("key=1.4不存在")
	}
}

// [复杂map] key 【不可以是 slice map function !!!】 ,key不能重复，key无序
//
// complexMap
//
//	@Description: 复杂map]
func complexMap() {
	fmt.Println("-------------复杂map-------------")
	//map的v是map
	var mapMap map[string]map[int]string = make(map[string]map[int]string)
	mapMap["class01"] = map[int]string{1: "zhangan", 2: "lisi", 3: "wangwu"}
	mapMap["class02"] = map[int]string{1: "tom", 2: "jack", 3: "lili", 4: "toms"}
	fmt.Println("mapMap=", mapMap)

	//map的v是array
	var arrayMap map[string][3]string = make(map[string][3]string)
	arrayMap["array01"] = [3]string{"zhangsan", "lisi", "wangwu"}
	arrayMap["array02"] = [3]string{"tom", "tom", "lili"}
	arrayMap["array03"] = [3]string{"刘备", "关羽", "张飞"}
	fmt.Println("arrayMap=", arrayMap)

	//map的v是slice
	var sliceMap map[string][]string = make(map[string][]string)
	sliceMap["slice01"] = []string{"zhangsan", "lisi", "wangwu"}
	sliceMap["slice02"] = []string{"tom", "tom", "lili"}
	sliceMap["slice03"] = []string{"刘备", "关羽", "张飞"}
	fmt.Println("sliceMap=", sliceMap)

	//map的v是struct
	var structMap map[string]Person = make(map[string]Person)
	structMap["p1"] = Person{name: "zhangsan", age: 12}
	structMap["p2"] = Person{name: "lisi", age: 12}
	structMap["p3"] = Person{name: "wangwu", age: 12}
	fmt.Println("structMap=", structMap)

	//map的v是[n]struct
	var structArrayMap map[string][2]Person = make(map[string][2]Person)
	structArrayMap["C1"] = [2]Person{{name: "zhangsan", age: 12}, {name: "lisi", age: 12}}
	structArrayMap["C2"] = [2]Person{{name: "tom", age: 12}, {name: "jack", age: 12}}
	fmt.Println("structArrayMap=", structArrayMap)

	//map的v是[]struct
	var structSliceMap map[string][]Person = make(map[string][]Person)
	structSliceMap["C1"] = []Person{{name: "zhangsan", age: 12}, {name: "lisi", age: 12}, {name: "lisi", age: 12}}
	structSliceMap["C2"] = []Person{{name: "tom", age: 12}, {name: "jack", age: 12}}
	fmt.Println("structSliceMap=", structSliceMap)
}

type Person struct {
	name string
	age  int
}
